package com.dzt.androidframework.json.car;

import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.dzt.androidframework.R;
import com.dzt.androidframework.activity.ActivityTitle;
import com.dzt.androidframework.utils.JToastKit;
import com.dzt.androidframework.utils.PinyinComparator;
import com.dzt.androidframework.widgets.PinnedHeaderListView;
import com.dzt.androidframework.widgets.SideBar;
import com.mob.mobapi.API;
import com.mob.mobapi.APICallback;
import com.mob.mobapi.MobAPI;
import com.mob.mobapi.apis.Car;

import net.sourceforge.pinyin4j.PinyinHelper;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class CarActivity extends ActivityTitle implements APICallback {

	@ViewInject(R.id.pinnedListView)
	private PinnedHeaderListView pinnedHeaderListView;
	@ViewInject(R.id.sb_find_brand)
	private SideBar sideBar;
	@ViewInject(R.id.dl_content)
	private DrawerLayout dlContent;
	@ViewInject(R.id.lv_right)
	private ListView lvRight;
	@ViewInject(R.id.tv_brand_name)
	private TextView tvBrandName;
	private RightDrawerListener drawerListener;
	private CarAdapter adapter;
	private List<CarBrand> carBrands;
	private CarSeriesAdapter carSeriesAdapter;
	private List<CarSeries> carSeries;
	private WindowManager windowManager;
	private TextView dlgText;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_car;
	}

	@Override
	protected void initParams() {
		setTitleView(R.string.text_car_info);
		carSeries = new ArrayList<>();
		carSeriesAdapter = new CarSeriesAdapter(this, carSeries);
		lvRight.setAdapter(carSeriesAdapter);
		dlContent.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
		drawerListener = new RightDrawerListener();
		dlContent.addDrawerListener(drawerListener);
		lvRight.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				Intent intent = new Intent();
				CarSeries series = carSeries.get(i);
				intent.putExtra("name", series.seriesName);
				startActivity(CarSeriesActivity.class, intent);
				drawerLayoutSwitch(false);
			}
		});

		carBrands = new ArrayList<>();
		adapter = new CarAdapter(this, carBrands);
		pinnedHeaderListView.setAdapter(adapter);
		sideBar.setListView(pinnedHeaderListView);
		pinnedHeaderListView.setOnScrollListener(adapter);
		pinnedHeaderListView.setPinnedHeaderView(LayoutInflater.from(this).inflate(
				R.layout.item_brand_header, pinnedHeaderListView, false));
		pinnedHeaderListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
				drawerLayoutSwitch(true);
				CarBrand carBrand = carBrands.get(i);
				carSeries.clear();
				carSeries.addAll(carBrand.subSeries);
				carSeriesAdapter.notifyDataSetChanged();
				tvBrandName.setText(carBrand.name);
			}
		});
		dlgText = (TextView) LayoutInflater.from(this).inflate(
				R.layout.fragment_find_brand_listview_remind, new LinearLayout(this), false);
		dlgText.setVisibility(View.INVISIBLE);
		// 初始华窗口管理器
		windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
				LayoutParams.TYPE_SYSTEM_ERROR,
				LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSPARENT);
		lp.gravity = Gravity.CENTER;
		// 将提示对话框添加到窗口中

		windowManager.addView(dlgText, lp);
		sideBar.setTextView(dlgText);
		Car api = (Car) MobAPI.getAPI(Car.NAME);
		api.queryBrand(this);
		showWaitDialog("加载中...", false, null);
	}

	@Override
	protected void initData(Bundle savedInstanceState) {

	}

	@Override
	protected String[] initPermissions() {
		return new String[0];
	}

	@Override
	public void onBackPressed() {
		if (dlContent.isDrawerOpen(Gravity.RIGHT)) {
			drawerLayoutSwitch(false);
		} else {
			super.onBackPressed();
			dlContent.removeDrawerListener(drawerListener);
			windowManager.removeView(dlgText);
		}
	}

	public void drawerLayoutSwitch(boolean isOpen) {
		if (isOpen) {
			dlContent.openDrawer(Gravity.RIGHT);
		} else {
			dlContent.closeDrawer(Gravity.RIGHT);
		}
	}

	@Override
	public void onSuccess(API api, int action, Map<String, Object> result) {
		dismissDialog();
		sideBar.setVisibility(View.VISIBLE);
		try {
			List<Map<String, Object>> res = (List<Map<String, Object>>) result.get("result");
			if (res == null || res.isEmpty()) {
				return;
			}
			for (Map<String, Object> tmp : res) {
				if (tmp == null || tmp.isEmpty()) {
					continue;
				}
				CarBrand carBrand = new CarBrand();
				String name = (String) tmp.get("name");
				carBrand.name = name;
				String[] array = PinyinHelper.toHanyuPinyinStringArray(name.charAt(0));
				if (array == null) {
					carBrand.firstLetter = String.valueOf(name.charAt(0));
				} else {
					carBrand.firstLetter = String.valueOf(array[0].charAt(0));
				}
				carBrand.firstLetter = carBrand.firstLetter.toUpperCase(
						Locale.CHINA);
				List<Map<String, Object>> son = (List<Map<String, Object>>) tmp.get("son");
				if (son == null || son.isEmpty()) {
					continue;
				}
				for (Map<String, Object> item : son) {
					CarSeries carSeries = new CarSeries();
					carSeries.subBrand = (String) item.get("car");
					carSeries.seriesName = (String) item.get("type");
					carBrand.subSeries.add(carSeries);
				}
				carBrands.add(carBrand);
			}
			Collections.sort(carBrands, new PinyinComparator());
			adapter.notifyDataSetChanged();
		} catch (Throwable t) {
			throw new RuntimeException(t);
		}
	}

	@Override
	public void onError(API api, int i, Throwable throwable) {
		dismissDialog();
		JToastKit.getInstance().showMessage("加载失败");
	}

	class RightDrawerListener implements DrawerLayout.DrawerListener {

		@Override
		public void onDrawerSlide(View drawerView, float slideOffset) {

		}

		@Override
		public void onDrawerOpened(View drawerView) {
			dlContent.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
		}

		@Override
		public void onDrawerClosed(View drawerView) {
			dlContent.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
		}

		@Override
		public void onDrawerStateChanged(int newState) {

		}
	}
}
